﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using MapInfo.MiPro.Interop;

namespace FloatingWindowTools.FloatingMapbasicWindow
{
    public partial class MapbasicWindowUI : Form
    {
        public MapbasicWindowUI(Window mapbasicWindow)
        {
            InitializeComponent();
            this.MapbasicWindow = mapbasicWindow;
            this.Load += new EventHandler(MapbasicWindow_Load);
            this.FormClosed += new FormClosedEventHandler(MapbasicWindow_FormClosed);
            this.Resize += new EventHandler(MapbasicWindow_Resize);
        }

        public Window MapbasicWindow { get; set; }

        void MapbasicWindow_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.MapbasicWindow.Close();
            MapInfo.MiPro.Interop.InteropServices.MapInfoApplication.Do("Alter Button ID 2005 Enable");
            // HACK: Enabling menu item isn't working.
            //MapInfo.MiPro.Interop.InteropServices.MapInfoApplication.Do("Alter Menu Item Load Enable");
        }

        void MapbasicWindow_Load(object sender, EventArgs e)
        {
            Window mapinfoWindow = new Window(InteropServices.MapInfoApplication, (int)Window.WindowType.WIN_MAPINFO);

            WinAPI.SetParent(this.Handle, mapinfoWindow.Handle);

            //Set the parent of Mpabsic window to a picture box on the form.
            this.MapbasicWindow.Parent = this.pictureBox1.Handle;
            this.MapbasicWindow.RemoveCaptionBar();
            this.MapbasicWindow.RemoveBorder();
            this.MapbasicWindow.Maximize();

            //Add one to the width to update the control.
            this.Width += 1;

            MapInfo.MiPro.Interop.InteropServices.MapInfoApplication.Do("Alter Button ID 2005 Disable");
            //MapInfo.MiPro.Interop.InteropServices.MapInfoApplication.Do("Alter Menu Item Load Disable");
        }

        private void MapbasicWindow_Resize(object sender, EventArgs e)
        {
            if (this.MapbasicWindow != null)
            {
                this.MapbasicWindow.UpdateSize(this.pictureBox1.Width, this.pictureBox1.Height);
            }
        }
    }
}
